/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#pragma once

#include "iroom.h"
#include "iroomitem.h"

#include <map>
#include <vector>
#include <string>
#include "coredll.h"

namespace greenhills {
namespace core {

/** @brief Holds room items on both, client and server side.
 *
 * This class holds room items and provides common functionality allowing to
 * add, remove or updated room item. It uses IRoomListener interface for sending
 * notification to all registered listeners.
 */
class GH_CORE_CLASS CRoom : public IRoom
{
private:
    typedef std::pair<core::CItemId, core::IRoomItem*> CRoomItemPair;
    typedef std::map<core::CItemId, core::IRoomItem*> CRoomItemMap;

public:
    typedef CRoomItemMap::const_iterator ConstItemIterator;
    typedef CRoomItemMap::iterator ItemIterator;

public:
    CRoom(const core::CRoomId &pRoomId, const std::string &pRoomName);

public:
    virtual std::string name() const {return mRoomName;}
    virtual core::CRoomId id() const {return mRoomId;}

    virtual ItemIterator begin() const;
    virtual ItemIterator end() const;

public: // IRoom
    virtual bool isEmpty() const;
    virtual core::IRoomItem *item(const core::CItemId &pItemId) const;
    virtual core::IRoomItem *itemAt(int index) const;
    virtual int indexOf(const core::CItemId &pItemId) const;
    virtual int count() const;

    virtual IRoomItem * createItem(const core::CItemId &pItemId,
            const std::string &pItemName, const core::CItemType &pItemType);
    virtual bool addItem(const core::CItemId &pItemId,
            const std::string &pItemName, const core::CItemType &pItemType);
    virtual bool updateItem(const core::CItemId &pItemId,
            const unsigned char *pData, unsigned int pLength);
    virtual bool removeItem(const core::CItemId &pItemId);

    virtual void registerListener(IRoomListener *pListener);
    virtual void unregisterListener(IRoomListener *pListener);
    virtual void unregisterAllListeners();

private:
    mutable CRoomItemMap mRoomItems;
    std::string mRoomName;
    core::CRoomId mRoomId;
    std::vector<core::IRoomListener*> mListeners;
};

} //namespace client
} //namespace greenhills
